package cn.ssm.controller;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ssm.pojo.Movie;
import cn.ssm.pojo.MovieCount;
import cn.ssm.service.MoviesDetailService;

@Controller
@RequestMapping("/moviesDetail")
/**
 * 电影类
 * 
 * @author 戴耀辉
 *
 */
public class MoviesDetailController {

	@Autowired
	private MoviesDetailService moviesDetailService;

	/**
	 * 查询所有电影信息
	 */
	@RequestMapping("/getMoviesDetailList")
	@ResponseBody
	public Object getMoviesDetailList() {
		Map<String, Object> map = new HashMap<String, Object>();

		List<Movie> moviesDetails = null;
		try {
			moviesDetails = moviesDetailService.getMoviesDetailList();
			if (moviesDetails != null) {
				map.put("result", moviesDetails);
				map.put("message", "查询电影成功！");
				map.put("success", true);
			} else {
				map.put("message", "未查询到该电影！");
				map.put("success", false);
			}
		} catch (Exception e) {
			map.put("message", "查询电影出现异常，无法正常查询！");
			map.put("success", false);
		}
		return map;
	}

	/**
	 * 模糊查询所有电影
	 */
	@RequestMapping("/selectMovies")
	@ResponseBody
	public Object selectMovies(String movieName) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			
			List<Movie> selectMovies = null;
			System.out.println("=======================" + movieName);
				selectMovies = moviesDetailService.selectMovies(movieName);
				if (selectMovies != null) {
					map.put("result", selectMovies);
					map.put("message", "查询电影成功！");
					map.put("success", true);
				} else {
					map.put("message", "未查询到该电影！");
					map.put("success", false);
				}
		}catch (Exception e) {
			map.put("message", "查询电影出现异常，无法正常查询！");
			map.put("success", false);
		}
		return map;
	}
	
	
	
	/**
	 * 查询类型电影
	 */
	@RequestMapping("/selectMovie")
	@ResponseBody
	public Object selectMovie(int movieTypeId) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			
			List<Movie> selectMovie = null;
			System.out.println("=======================" + movieTypeId);
			selectMovie = moviesDetailService.selectMovie(movieTypeId);
			if (selectMovie != null) {
				map.put("result", selectMovie);
				map.put("message", "查询电影成功！");
				map.put("success", true);
			} else {
				map.put("result", selectMovie);
				map.put("message", "未查询到该电影！");
				map.put("success", false);
			}
		}catch (Exception e) {
			map.put("message", "查询电影出现异常，无法正常查询！");
			map.put("success", false);
		}
		return map;
	}
	
	/**
	 * 查询未上映电影信息
	 */
	@RequestMapping("/getNewMoviesList")
	@ResponseBody
	public Object getNewMoviesList() {
		Map<String, Object> map = new HashMap<String, Object>();

		List<Movie> moviesDetails = null;
		try {
			moviesDetails = moviesDetailService.getNewMoviesList();
			if (moviesDetails != null) {
				map.put("result", moviesDetails);
				map.put("message", "查询电影成功！");
				map.put("success", true);
				map.put("size", moviesDetails.size());
			} else {
				map.put("message", "未查询到该电影！");
				map.put("success", false);
			}
		} catch (Exception e) {
			map.put("message", "查询电影出现异常，无法正常查询！");
			map.put("success", false);
		}
		return map;
	}
	

	/**
	 * 查询ID对应的电影信息
	 */
	@RequestMapping("/getMovieId")
	@ResponseBody
	public Object getMovieId(int id) {
		Map<String, Object> map = new HashMap<String, Object>();

		List<Movie> movieId = null;
		try {
			movieId = moviesDetailService.getMovieId(id);
			if (movieId != null) {
				map.put("result", movieId);
				map.put("message", "查询电影成功！");
				map.put("success", true);
			} else {
				map.put("message", "未查询到该电影！");
				map.put("success", false);
			}
		} catch (Exception e) {
			map.put("message", "查询电影出现异常，无法正常查询！");
			map.put("success", false);
		}
		return map;

	}

	@RequestMapping("/getMoviesList")
	@ResponseBody
	public Object getMovieslList() {
		Map<String, Object> map = new HashMap<String, Object>();

		List<Movie> moviesList = null;
		try {
			moviesList = moviesDetailService.getMoviesList();
			if (moviesList != null) {
				map.put("result", moviesList);
				map.put("message", "查询电影成功！");
				map.put("success", true);
			} else {
				map.put("message", "未查询到电影！");
				map.put("success", false);
			}
		} catch (Exception e) {
			map.put("message", "查询电影出现异常，无法正常查询！");
			map.put("success", false);
		}
		return map;
	}
	
	//根据id查询电影  艾辉
	@RequestMapping("/getMovieById")
	@ResponseBody
	public Object getMovieById(Integer id){
		Map<String,Object> map1 = new HashMap<String, Object>();
		List<Movie> movies =null;
		
		/*try{*/
			movies = moviesDetailService.getMovieById(id);
			map1.put("result", movies);
			map1.put("message", "查询成功！");
			map1.put("success", true);
		/*}catch(Exception e){
			map1.put("message","查询失败！");
			map1.put("success", false);
		}*/
		return map1;
	}
	
	//<!-- 查询每部热映电影的订单总量  黄少涛 -->
	@RequestMapping("/getMovieOrderCount")
	@ResponseBody
	public Object getMovieOrderCount() {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("success", false);
		List<String> names = new ArrayList<String>();  //储存单独的电影名
		try {
			
			List<MovieCount> movieOrderCounts = moviesDetailService.getMovieOrderCount();
			for (MovieCount movieCount : movieOrderCounts) {
				String name = movieCount.getName();
				names.add(name);
			}
			map.put("results", movieOrderCounts);
			map.put("result", names);
			map.put("message", "查询成功！");
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("message", "查询出现异常！");
		}
		return map ;
	}
	
	//查询电影总数
		@RequestMapping("/getMoviesDetail")
		@ResponseBody
		public int getAdminCount(){
			return moviesDetailService.getMoviesDetail();
		}
		
		/**
		 * 分页查询所有电影信息
		 * @return
		 */
		@RequestMapping("/getMovies")
		@ResponseBody
		public Object getMovies(Integer nowPage, Integer size) {
			//接受的参数
			Map<String, Object> paramas = new HashMap<String, Object>();
			if(nowPage !=null || size !=null){
				paramas.put("start", (nowPage-1)*size);
				paramas.put("size", size);
			}
			
			//返回的结果
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("success", false);
			
			try{
				List<Movie> allMoviesList = moviesDetailService.getMovies(paramas);
				map.put("result", allMoviesList);
				map.put("message", "查询用户成功");
				map.put("success", true);
			}catch(Exception e){
				map.put("result", "查询用户出现异常");
			}
			return map;
		}
		
		//查询电影总数
		@RequestMapping("/getMoviesCount")
		@ResponseBody
		public int getMoviesCount(){
			return moviesDetailService.getMoviesCount();
		}
		
		
		//删除电影信息
		@RequestMapping("/deleteMovies")
		@ResponseBody
		public int deleteMovies(@Param("id")Integer id){
			
			return moviesDetailService.deleteMovies(id);
			
		}	
		
		
		//存储电影
		@RequestMapping("/saveMovies.htm")
		@ResponseBody
		public Object saveMovies(Movie m,HttpServletRequest request,HttpServletResponse response){
			String date = m.getReleaseDate();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date parse = null;
			try {			
				//使用SimpleDateFormat的parse()方法生成Date			
				 parse = sf.parse(date);	
				//打印Date		
				} catch (ParseException e) {			
					e.printStackTrace();		
				}
		
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("movieName",m.getMovieName());
			params.put("movieTypeId",m.getMovieTypeId());
			params.put("movieIsActiveId",m.getMovieIsActiveId());
			params.put("protagonist",m.getProtagonist());
			params.put("director",m.getDirector());
			params.put("duration",m.getDuration());
			params.put("description",m.getDescription());
			params.put("poster",m.getPoster());
			params.put("state",m.getState());
			params.put("releaseDate",parse);
			params.put("issuingCompany",m.getIssuingCompany());
			
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("success", false);
			int count = moviesDetailService.saveMovies(params);
			try{
				if(count>0){
					response.sendRedirect("/moviesMaven/view/index.html");
					map.put("message", "添加成功！");
					map.put("success", true);
				}else{
					map.put("message", "添加失败！");
				}
			}catch(Exception e){
				map.put("result", "查询用户出现异常");
			}
			return map;
		}
}
